﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GsnHelper;
using ZhongheCms.Models;
namespace ZhongheCms.Controllers
{
    public class MemberInfoController : Controller
    {
        //
        // GET: /MemberInfo/

        public ActionResult Index(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblMember.GetMembers(sdate, edate, keywords,GswTblUser.CurrentUser.CustomerId,pageIndex);
            return View(qry);
        }
        public ActionResult MemberBrowse(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var member = GswTblMember.SingleOrDefault(id);
            member.BaseInfos = GswTblBaseInfo.Fetch("where FunctionId=76 and CustomerId=19");
            member.JobWorkAllp = GswViewJobWorkAll.Fetch("where AcceptedFlag=1 and  MemberId=" + id + " order by RegDate desc");
            return View(member);
        }
        public ActionResult MemberView(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var member = GswTblMember.SingleOrDefault(id);
            member.BaseInfos = GswTblBaseInfo.Fetch("where FunctionId=76 and CustomerId=19");
           member.JobWorkAllp = GswViewJobWorkAll.Fetch("where AcceptedFlag=1 and  MemberId=" + id + " order by RegDate desc");
          
            return View(member);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult MemberView(FormCollection c,int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            
            var member = GswTblMember.SingleOrDefault(id);
            member.MemberType = c["MemberType"];
            member.MemberCardNo = c["MemberCardNo"];
            member.MemberDesc = c["MemberDesc"];
            member.MemberPoint = InputValid.StrToInt(c["MemberPoint"], 0);
            member.AuditFlag = InputValid.StrToInt(c["AuditFlag"], 0);
            try
            {
                member.Update();
                ViewBag.scode = "1";
            }
            catch (Exception e)
            {

                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            member.BaseInfos = GswTblBaseInfo.Fetch("where FunctionId=76 and CustomerId=19");
          
            member.JobWorkAllp = GswViewJobWorkAll.Fetch("where AcceptedFlag=1 and  MemberId=" + id + " order by RegDate desc");
            return View(member);
        }
        public ActionResult Delete(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblMember.Delete(id);
            var qry = GswTblMember.GetMembers(sdate, edate, keywords, GswTblUser.CurrentUser.CustomerId,pageIndex);
            return View("Index",qry);
        }

        public ActionResult WorkTime(int id = 0,int pageIndex=1 ,string m = "", string f = "", string p = "", string fid = "", string r = "", string wt = "",string wd="")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.wt = wt;
            ViewBag.wd = wd;
            ViewBag.pageIndex = pageIndex;
            int flag = 0;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var sql = "select * from GswViewMemberTime where ParentId=" + GswTblUser.CurrentUser.CustomerId;
            if (!string.IsNullOrEmpty(wt))
            {
                flag = 1;
                sql += " and WorkTime='" + wt + "' and (d1>0 or d2>0 or d3>0 or d4>0 or d5>0 or d6>0 or d7>0)";
            }
            if (!string.IsNullOrEmpty(wd))
            {
                flag = 1;
                sql += " and "+ wd+ ">0";
                switch (wd)
                {
                    case "D1":
                        ViewBag.wdn = "星期一";
                        break;
                    case "D2":
                        ViewBag.wdn = "星期二";
                        break;
                    case "D3":
                        ViewBag.wdn = "星期三";
                        break;
                    case "D4":
                        ViewBag.wdn = "星期四";
                        break;
                    case "D5":
                        ViewBag.wdn = "星期五";
                        break;
                    case "D6":
                        ViewBag.wdn = "星期六";
                        break;
                    case "D7":
                        ViewBag.wdn = "星期日";
                        break;

                }
            }
            if(flag==0)
                sql += " and LastDate>='" + DateTime.Today.AddDays(-30) + "'";
            sql += " order by LastDate desc , MemberId, WorkTime";
            var db = new ZhongheRepo();
            var qry = db.Page<GswViewMemberTime>(pageIndex, 20, sql); //GswViewMemberTime.Fetch(sql);
            return View(qry);
        }
    }
}
